using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace MoEngine
{
    public class WindowStyle
    {
        protected Texture2D m_TextureCorners;
        protected Texture2D m_TextureSidesHorizontal;
        protected Texture2D m_TextureSidesVertical;
        protected Texture2D m_TextureCenter;
        
        protected int m_MarginLeft;
        protected int m_MarginRight;
        protected int m_MarginTop;
        protected int m_MarginBottom;

        public WindowStyle(Texture2D rawTexture, Rectangle cenRect)
        {
            // create texture for corners
            m_TextureCorners = new Texture2D(Core.GlobalDevice, rawTexture.Width - cenRect.Width, rawTexture.Height - cenRect.Height, 1, TextureUsage.None, SurfaceFormat.Color);

            m_MarginLeft = cenRect.X;
            m_MarginRight = rawTexture.Width - cenRect.Width - m_MarginLeft;
            m_MarginTop = cenRect.Y;
            m_MarginBottom = rawTexture.Height - cenRect.Height - m_MarginTop;


            // top left corner
            int width = m_MarginLeft;
            int height = m_MarginTop;
            Color[] data = new Color[width * height];
            Rectangle rect = new Rectangle(0, 0, width, height);
            rawTexture.GetData<Color>(0, rect, data, 0, data.Length);
            m_TextureCorners.SetData<Color>(0, rect, data, 0, data.Length, SetDataOptions.None);

            // top right corner
            width = m_MarginRight;
            height = m_MarginTop;
            data = new Color[width * height];
            rawTexture.GetData<Color>(0, new Rectangle(cenRect.Right, 0, width, height), data, 0, data.Length);
            m_TextureCorners.SetData<Color>(0, new Rectangle(cenRect.X, 0, width, height), data, 0, data.Length, SetDataOptions.None);

            // bottom left corner
            width = m_MarginLeft;
            height = m_MarginBottom;
            data = new Color[width * height];
            rawTexture.GetData<Color>(0, new Rectangle(0, cenRect.Bottom, width, height), data, 0, data.Length);
            m_TextureCorners.SetData<Color>(0, new Rectangle(0, cenRect.Y, width, height), data, 0, data.Length, SetDataOptions.None);

            // bottom right corner
            width = m_MarginRight;
            height = m_MarginBottom;
            data = new Color[width * height];
            rawTexture.GetData<Color>(0, new Rectangle(cenRect.Right, cenRect.Bottom, width, height), data, 0, data.Length);
            m_TextureCorners.SetData<Color>(0, new Rectangle(cenRect.X, cenRect.Y, width, height), data, 0, data.Length, SetDataOptions.None);

            // create textures for horizontal sides
            m_TextureSidesHorizontal = new Texture2D(Core.GlobalDevice, cenRect.Width, m_MarginTop+m_MarginBottom, 1, TextureUsage.None, SurfaceFormat.Color);
            // top side
            width = cenRect.Width;
            height = m_MarginTop;
            data = new Color[width * height];
            rawTexture.GetData<Color>(0, new Rectangle(cenRect.X, 0, width, height), data, 0, data.Length);
            m_TextureSidesHorizontal.SetData<Color>(0, new Rectangle(0, 0, width, height), data, 0, data.Length, SetDataOptions.None);

            // bottom side
            width = cenRect.Width;
            height = m_MarginTop;
            data = new Color[width * height];
            rawTexture.GetData<Color>(0, new Rectangle(cenRect.X, m_MarginTop+cenRect.Height, width, height), data, 0, data.Length);
            m_TextureSidesHorizontal.SetData<Color>(0, new Rectangle(0, m_MarginTop, width, height), data, 0, data.Length, SetDataOptions.None);

            // create textures for vertical sides
            m_TextureSidesVertical = new Texture2D(Core.GlobalDevice, m_MarginLeft+m_MarginRight, cenRect.Height, 1, TextureUsage.None, SurfaceFormat.Color);
            // left side
            width = m_MarginLeft;
            height = cenRect.Height;
            data = new Color[width * height];
            rawTexture.GetData<Color>(0, new Rectangle(0, cenRect.Y, width, height), data, 0, data.Length);
            m_TextureSidesVertical.SetData<Color>(0, new Rectangle(0, 0, width, height), data, 0, data.Length, SetDataOptions.None);

            // right side
            width = m_MarginRight;
            height = cenRect.Height;
            data = new Color[width * height];
            rawTexture.GetData<Color>(0, new Rectangle(m_MarginLeft+cenRect.Width, cenRect.Y, width, height), data, 0, data.Length);
            m_TextureSidesVertical.SetData<Color>(0, new Rectangle(m_MarginLeft, 0, width, height), data, 0, data.Length, SetDataOptions.None);

            
            // create textures for center
            m_TextureCenter = new Texture2D(Core.GlobalDevice, cenRect.Width, cenRect.Height, 1, TextureUsage.None, SurfaceFormat.Color);
            width = cenRect.Width;
            height = cenRect.Height;
            data = new Color[width * height];
            rawTexture.GetData<Color>(0, cenRect, data, 0, data.Length);
            m_TextureCenter.SetData<Color>(data);
        }

        public virtual void Draw(SpriteBatch spriteBatch, Rectangle rect)
        {
            // draw corners
            spriteBatch.Draw(m_TextureCorners, 
                new Rectangle(rect.X, rect.Y, m_MarginLeft, m_MarginTop),
                new Rectangle(0, 0, m_MarginLeft, m_MarginTop), 
                Color.White);
            spriteBatch.Draw(m_TextureCorners, 
                new Rectangle(rect.Right - m_MarginRight, rect.Y, m_MarginRight, m_MarginTop),
                new Rectangle(m_MarginLeft, 0, m_MarginRight, m_MarginTop), 
                Color.White);
            spriteBatch.Draw(m_TextureCorners, 
                new Rectangle(rect.X, rect.Bottom - m_MarginBottom, m_MarginLeft, m_MarginBottom),
                new Rectangle(0, m_MarginTop, m_MarginLeft, m_MarginBottom), 
                Color.White);
            spriteBatch.Draw(m_TextureCorners,
                new Rectangle(rect.Right - m_MarginRight, rect.Bottom - m_MarginBottom, m_MarginRight, m_MarginBottom),
                new Rectangle(m_MarginLeft, m_MarginTop, m_MarginRight, m_MarginBottom), 
                Color.White);

            // draw sides
            spriteBatch.Draw(m_TextureSidesHorizontal,
                new Rectangle(rect.X + m_MarginLeft, rect.Y, rect.Width - m_MarginLeft - m_MarginRight, m_MarginTop),
                new Rectangle(0, 0, rect.Width - m_MarginLeft - m_MarginRight, m_MarginTop),
                Color.White);
            spriteBatch.Draw(m_TextureSidesHorizontal,
                new Rectangle(rect.X + m_MarginLeft, rect.Bottom - m_MarginBottom, rect.Width - m_MarginLeft - m_MarginRight, m_MarginBottom),
                new Rectangle(0, m_MarginTop, rect.Width - m_MarginLeft - m_MarginRight, m_MarginBottom),
                Color.White);

            spriteBatch.Draw(m_TextureSidesVertical,
                new Rectangle(rect.X, rect.Y + m_MarginTop, m_MarginLeft, rect.Height - m_MarginTop - m_MarginBottom),
                new Rectangle(0, 0, m_MarginLeft, rect.Height - m_MarginTop - m_MarginBottom),
                Color.White);
            spriteBatch.Draw(m_TextureSidesVertical,
                new Rectangle(rect.Right - m_MarginRight, rect.Y + m_MarginTop, m_MarginRight, rect.Height - m_MarginTop - m_MarginBottom),
                new Rectangle(m_MarginLeft, 0, m_MarginRight, rect.Height - m_MarginTop - m_MarginBottom),
                Color.White);

            // center piece
            spriteBatch.Draw(m_TextureCenter,
                new Rectangle(rect.X + m_MarginLeft, rect.Y + m_MarginTop, rect.Width - m_MarginLeft - m_MarginRight, rect.Height - m_MarginTop - m_MarginBottom),
                new Rectangle(0, 0, rect.Width - m_MarginLeft - m_MarginRight, rect.Height - m_MarginTop - m_MarginBottom),
                Color.White);

        }
    }
}
